<?php

include 'confidential_settings.php';
// This include contains 5 variables:
//  - db_name: database name
//  - table_name_x: table name
//  - username: username
//  - password: password
//  - localhost: host name

function get_corr_value_year($year){
	include 'gas_corrections.php';

	$ret_corr_value = 0;
	for($i=0;$i<count($corr_year);$i++){
		if(trim($corr_year[$i]) == trim($year)){
			$ret_corr_value = $ret_corr_value + $corr_value[$i];
		}
	}
	return $ret_corr_value;
}

$dbh=mysql_connect ($localhost, $username, $password) or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db_name) or die(mysql_error()); 

$result = mysql_query("SELECT YEAR(date_entered), SUM(val) FROM $table_name_gas GROUP BY YEAR(date_entered)");

mysql_close($dbh);


mysql_data_seek ($result , 0);

$max = 0;
$nb = 0;
while($row = mysql_fetch_array($result)){
	$tmp = $row['SUM(val)']/100;
	$corr_value_tmp = get_corr_value_year($row['YEAR(date_entered)']);
	$tmp = $tmp + $corr_value_tmp;
	if($tmp > $max){
		$max = $tmp;
	}
	$nb = $nb + 1;
}
$max = $max * 1.05;

mysql_data_seek ($result , 0);

echo '<img src="http://chart.apis.google.com/chart?cht=bvs&chd=t:';
if($row = mysql_fetch_array($result)){
	$tmp = $row['SUM(val)']/100;
	$corr_value_tmp = get_corr_value_year($row['YEAR(date_entered)']);
	$tmp = ($tmp+$corr_value_tmp) *100/$max;
	echo $tmp;
}
while($row = mysql_fetch_array($result)){
	$tmp = $row['SUM(val)']/100;
	$corr_value_tmp = get_corr_value_year($row['YEAR(date_entered)']);
	$tmp = ($tmp+$corr_value_tmp) *100/$max;
	echo ',';
	echo $tmp;
}
echo '&chs=600x300&chg=20,20&chxt=x,y&chxr=1,0,';
echo $nb;
echo '|1,0,';
echo $max;

mysql_data_seek ($result , 0);

echo '&chxl=0:|';
while($row = mysql_fetch_array($result)){
	echo $row['YEAR(date_entered)'];
	echo '|';
}

echo '" />';

?>
